jQuery(document).ready(function () {

    $.gradeTableMgrFormatter = {
        stateFormatter:function(value,row,index){
            if(value == '1'){
                return '<span class="badge">不生效</span>'
            } else {
                return '<span class="badge badge-success">生效</span>'
            }
        }
    };
    $.gradeOperator = {
        state:[{id:0,text:"生效"},{id:1,text:"不生效"}],
        validOptions: {
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            message: '值未验证通过',
            group: '.v-group',
            fields: {
                cnNameForm: {validators: {notEmpty: {message: '请填写年级名称'}}},
                stateForm: {validators: {notEmpty: {message: '请选择状态'}}}
            }
        },
        getForm: function () {
            var $tableDataDiv = $('<form id="tableForm" role="form" class="form-horizontal"></form>');
            var $tableInfo = $('<div class="form-group"></div>');
            $tableInfo.append('<div class="v-group"><label for="gradeName-form" class="col-md-2 col-sm-2 col-xs-2 control-label">年级名称</label>' +
                '<div class="col-md-4 col-sm-4 col-xs-10 ">' +
                '<input type="text" id="gradeName-form" class="form-control" name="cnNameForm" placeholder="年级名称"/></div></div>');
            $tableInfo.append('<div class="v-group">' +
                '<label for="state-form" class="col-md-2 col-sm-2 col-xs-2 control-label">状态</label>' +
                '<div class="col-md-4 col-sm-4 col-xs-10 ">' +
                '<select class="form-control select2" id="state-form" name="stateForm" placeholder="状态"></select>' +
                '</div></div>');
            $tableDataDiv.append($tableInfo);
            return $tableDataDiv;
        },
        refreshData: function () {
            $('#gradeTable').bootstrapTable('refresh');
            $('#gradeTable').bootstrapTable('resetView');
        },
        addGradeOperator : function () {
            BootstrapDialog.show({
                size: BootstrapDialog.SIZE_WIDE,
                draggable: true,
                title: '新增年级信息',
                message: $.gradeOperator.getForm(false),
                buttons: [{
                    label: '提交',
                    action: function (dialogRef) {
                        var validate = $("#tableForm").data('bootstrapValidator').validate();
                        if (!validate.isValid()) {
                            return;
                        }

                        $.ajax({
                            url: basePath + 'educational/grade/saveGrade.do',
                            type: 'POST',
                            data: {
                                name: $('#tableForm #gradeName-form').val(),
                                state: $('#tableForm #state-form').val()
                            },
                            async: true,
                            dataType: 'json',
                            beforeSend: function () {
                                MaskLoading.fullScreenShow("正在提交,请稍后...");
                            },
                            success: function (data) {
                                if (data.success) {
                                    noty({
                                        text: "新增数据成功",
                                        layout: "center",
                                        type: "success",
                                        timeout: 1000,
                                        theme: 'bootstrapTheme'
                                    });
                                    MaskLoading.fullScreenHide();
                                    dialogRef.close();
                                    $.gradeOperator.refreshData();
                                } else {
                                    noty({
                                        text: data.message.errorMessage,
                                        layout: "center",
                                        type: "error",
                                        timeout: 1000,
                                        theme: 'bootstrapTheme'
                                    });
                                    MaskLoading.fullScreenHide();
                                }
                            },
                            error: function (data) {
                                noty({
                                    text: "服务器异常",
                                    layout: "center",
                                    type: "error",
                                    timeout: 1000,
                                    theme: 'bootstrapTheme'
                                });
                                MaskLoading.fullScreenHide();
                            },
                            complete: function () {
                                MaskLoading.fullScreenHide();
                            }
                        });
                    }
                }, {
                    label: '关闭',
                    action: function (dialogRef) {
                        dialogRef.close();
                    }
                }],

                onshown: function (dialogRef) {
                    $('#state-form').select2({
                        placeholder: '请选择状态',
                        width: "100%",
                        data: $.gradeOperator.state,
                        minimumResultsForSearch: -1,
                        multiple: false
                    });

                    $("#tableForm").bootstrapValidator($.gradeOperator.validOptions);
                }
            });
        },
        updateGradeOperator : function(){
            var selections = $('#gradeTable').bootstrapTable('getSelections');
            if (selections.length == 0) {
                noty({
                    text: "请勾选需要操作的记录",
                    layout: "center",
                    type: "error",
                    timeout: 1000,
                    theme: 'bootstrapTheme'
                });
                return false;
            }
            if (selections.length > 1) {
                noty({
                    text: "只能勾选一条需要操作的记录",
                    layout: "center",
                    type: "error",
                    timeout: 1000,
                    theme: 'bootstrapTheme'
                });
                return false;
            }
            BootstrapDialog.show({
                size: BootstrapDialog.SIZE_WIDE,
                draggable: true,
                title: '修改年级信息',
                message: $.gradeOperator.getForm(true),
                buttons: [{
                    label: '提交',
                    action: function (dialogRef) {
                        $.ajax({
                            url: basePath + 'educational/grade/saveGrade.do',
                            type: 'POST',
                            data: {
                                name: $('#tableForm #gradeName-form').val(),
                                state: $('#tableForm #state-form').val(),
                                gradeId: selections[0].gradeId
                            },
                            async: true,
                            dataType: 'json',
                            beforeSend: function () {
                                MaskLoading.fullScreenShow("正在提交,请稍后...");
                            },
                            success: function (data) {
                                if (data.success) {
                                    noty({
                                        text: "修改数据成功",
                                        layout: "center",
                                        type: "success",
                                        timeout: 1000,
                                        theme: 'bootstrapTheme'
                                    });
                                    MaskLoading.fullScreenHide();
                                    dialogRef.close();
                                    $.gradeOperator.refreshData();
                                } else {
                                    noty({
                                        text: data.message.errorMessage,
                                        layout: "center",
                                        type: "error",
                                        timeout: 1000,
                                        theme: 'bootstrapTheme'
                                    });
                                    MaskLoading.fullScreenHide();
                                }
                            },
                            error: function (data) {
                                noty({
                                    text: "服务器异常",
                                    layout: "center",
                                    type: "error",
                                    timeout: 1000,
                                    theme: 'bootstrapTheme'
                                });
                                MaskLoading.fullScreenHide();
                            },
                            complete: function () {
                                MaskLoading.fullScreenHide();
                            }
                        });
                    }
                }, {
                    label: '关闭',
                    action: function (dialogRef) {
                        dialogRef.close();
                    }
                }],
                onshown: function (dialogRef) {
                    $('#state-form').select2({
                        placeholder: '请选择状态',
                        allowClear: true,
                        width: "100%",
                        data: $.gradeOperator.state,
                        minimumResultsForSearch: -1,
                        multiple: false
                    });

                    $("#state-form").val(selections[0].state).trigger("change");
                    $("#gradeName-form").val(selections[0].gradeName);
                    $("#tableForm").bootstrapValidator($.gradeOperator.validOptions);
                }
            });
        },
        delGradeOperator : function () {
            var selections = $('#gradeTable').bootstrapTable('getSelections');
            if (selections.length == 0) {
                noty({
                    text: "请勾选需要操作的记录",
                    layout: "center",
                    type: "error",
                    timeout: 1000,
                    theme: 'bootstrapTheme'
                });
                return false;
            }
            if (selections.length > 1) {
                noty({
                    text: "只能勾选一条需要操作的记录",
                    layout: "center",
                    type: "error",
                    timeout: 1000,
                    theme: 'bootstrapTheme'
                });
                return false;
            }

            BootstrapDialog.confirm({
                draggable: true,
                btnOKLabel: '是',
                title: "删除菜单",
                btnCancelLabel: '否',
                message: '确定要进行删除操作吗?',
                callback: function (result) {
                    if (result) {
                        $.ajax({
                            url: basePath+'educational/grade/deleteGrade.do',
                            type: 'POST',
                            data: {
                                gradeId: selections[0].gradeId
                            },
                            async: true,
                            dataType: 'json',
                            beforeSend: function () {
                                MaskLoading.fullScreenShow("正在提交,请稍后...");
                            },
                            success: function (data) {
                                if (data.success) {
                                    noty({
                                        text: "删除数据成功",
                                        layout: "center",
                                        type: "success",
                                        timeout: 1000,
                                        theme: 'bootstrapTheme'
                                    });
                                    MaskLoading.fullScreenHide();
                                    $.gradeOperator.refreshData();
                                } else {
                                    noty({
                                        text: data.message.errorMessage,
                                        layout: "center",
                                        type: "error",
                                        timeout: 1000,
                                        theme: 'bootstrapTheme'
                                    });
                                    MaskLoading.fullScreenHide();
                                }
                            },
                            error: function (data) {
                                noty({
                                    text: "服务器异常",
                                    layout: "center",
                                    type: "error",
                                    timeout: 1000,
                                    theme: 'bootstrapTheme'
                                });
                            },
                            complete: function () {
                                MaskLoading.fullScreenHide();
                            }
                        });
                    }
                }
            });
        }
    };
    $.gradeTable = {
        state:[{id:0,text:"生效"},{id:1,text:"不生效"}],
        initButton:function(){
            $("#addData").on('click', $.gradeOperator.addGradeOperator);
            $("#updateData").on('click', $.gradeOperator.updateGradeOperator);
            $("#delData").on('click', $.gradeOperator.delGradeOperator);
        },

        initTable:function(){
            $('#mainContent').show();
            $('#gradeTable').bootstrapTable('destroy');
            $('#gradeTable').bootstrapTable({
                height: '100%',
                method: 'get',
                url : basePath + 'educational/grade/getGradeAll.do',
                dataType: "json",
                striped: true,// 使表格有条纹
                pagination: true,// 在表格底部显示分页
                pageNumber: 1,
                pageSize: 20,
                cache: false,// 是否使用缓存
                pageList: [20, 50, 100],
                paginationLoop: false,
                idField: 'gradeId',// 标识哪个字段为id主键
                sidePagination: "client",// 表格分页的位置
                queryParamsType: "", // 参数格式,发送标准的RESTFul类型的参数请求
                clickToSelect: true,//点击行即可选中单选/复选框
                sortable: true,
                showRefresh: true,
                toolbar: '#toolbar',
                search: true,//是否显示右上角的搜索框
                queryParams: function (params) {

                }, //参数
                columns: [{
                    checkbox: true
                }, {
                    field: 'gradeId',
                    title: '年级ID',
                    visible: false
                }, {
                    field: 'gradeName',
                    title: '名称'
                }, {
                    field: 'state',
                    title: '状态',
                    formatter: $.gradeTableMgrFormatter.stateFormatter
                }]
            });
            $('#gradeTable').bootstrapTable(
                'resetView',{
                    height: $(window).height() - 70
                }
            );
        }
    };

    $(function () {
        $.gradeTable.initButton();
        $.gradeTable.initTable();
    })
});